//
//  SQHttpManager.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/11/28.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit
import Alamofire


let weiboKey = "c6fdfd2e65837ccaf50a39dda3609cb4"

class SQHttpManager: NSObject {
    static let shared = SQHttpManager()
    
    var access_token: String?{
        return SQUserAccountManager.shared.userInfo?.access_token
    }
    var uid: String? = "2867350594"
    
    let client_id = "65802674"
    
    let redirect_uri = "http://my.dingdone.com/share/callback.php"
    
    let client_secret = "32905027b41f4854bd232ee469845b4a"
    
    private override init() {}
    
//    https://api.weibo.com/oauth2/access_token?client_id=65802674&client_secret=32905027b41f4854bd232ee469845b4a&grant_type=authorization_code&code=227a0aa3cb32abde0f62d3187f33ed8d&redirect_uri=http://my.dingdone.com/share/callback.php
//    
//    {"access_token":"2.00OsGDID09RG9E10a4c95bfcIA9CJE",
//    "remind_in":"157679999",
//    "expires_in":157679999,
//    "uid":"2867350594"}
    
    open func request(url:String,
                        method:HTTPMethod = .get,
                        params:[String:String]?=nil,
                        headers:[String:String]?=nil,
                        success:@escaping (_ data:Any?)->Void,
                        failure:(_ msg:String, _ code:String)->Void)
    {
        
        Alamofire.request(url,
                          method:method,
                          parameters:params,
                          headers:headers).responseJSON { (data) in
                            
                            success(data.result.value)
                            
        }
    }
    
    open func post(url:String,
                      params:[String:String]?=nil,
                      headers:[String:String]?=nil,
                      success:@escaping (_ data:Any?)->Void,
                      failure:(_ msg:String, _ code:String)->Void)
    {
        
        Alamofire.request(url,
                          method:.post,
                          parameters:params,
                          headers:headers).responseJSON { (data) in
                            
                            success(data.result.value)
                            
        }
    }
    
    
    
    
    
}
